// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef HUAWEICLOUD_CLOUDPHONEACCESS_ENGINE_CAEVIRTUALDEVICEDATAHANDLERTHREAD_H
#define HUAWEICLOUD_CLOUDPHONEACCESS_ENGINE_CAEVIRTUALDEVICEDATAHANDLERTHREAD_H

#include "../cae_common/CaeThread.h"
#include "../cae_dataPipe/CaeDataPipe.h"
#include "CaeLocalDataHandleThread.h"

constexpr int VITURAL_DEVICE_HANDLER_THREAD_SLEEP_INTERVAL = 1000;

using VirtualDeviceSendHook = std::function<int (void *pkt, size_t size)>;

class CaeVirtualDeviceDataHandlerThread : public CaeThread {
public:
    CaeDataPipe* m_virtualDeviceData;
    CaeVirtualDeviceDataHandlerThread(CaeDataPipe* pipe);
    ~CaeVirtualDeviceDataHandlerThread();
    void Run() override;

    void HandleStartPreviewReq(MSG_HEADER *header, uint8_t *body, CaePipeData *pipeData);
    void HandleStartRecordReq(MSG_HEADER *header, uint8_t *body, CaePipeData *pipeData);

    void SetVirutalDeviceSendHook(const VirtualDeviceSendHook & hook);

private:
    CaeLocalDataHandleThread* m_cameraLocalDataHandleThread;
    CaeLocalDataHandleThread* m_microphoneLocalDataHandleThread;
    int m_width = 1280;
    int m_height = 720;
    int m_sampleRate = 48000;

    VirtualDeviceSendHook m_sendHook = nullptr;

    int HandleDataToVirtualDevice(CaePipeData *pipeData, uint8_t type);
    bool GetLocalH264StreamParams();
    void HandleCameraMsg(MSG_HEADER header, uint8_t *data, CaePipeData *pipeData);
    void HandleMicrophoneMsg(MSG_HEADER header, uint8_t *data, CaePipeData *pipeData);
    void HandleGetInfoReq(MSG_HEADER *header);
    void HandleGetParamReq(MSG_HEADER *header);
    void HandleCameraSetParamReq(MSG_HEADER *header, uint8_t *body);
    void HandleStopPreviewReq(MSG_HEADER *header, CaePipeData *pipeData);
    void HandleGetSizeReq(MSG_HEADER *header);

    void HandleMicrophoneSetParamReq(MSG_HEADER *header, uint8_t *body, CaePipeData *pipeData);
    void HandleStopRecordReq(MSG_HEADER *header, uint8_t *body, CaePipeData *pipeData);

};


#endif //HUAWEICLOUD_CLOUDPHONEACCESS_ENGINE_CAEVIRTUALDEVICEDATAHANDLERTHREAD_H
